package com.molichuxing.gateway.bff.dealer.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.gateway.bff.dealer.convertor.Convert;
import com.molichuxing.gateway.bff.dealer.entity.response.DealerEsignInfoVo;
import com.molichuxing.gateway.bff.dealer.entity.response.DealerGoodsVo;
import com.molichuxing.gateway.bff.dealer.entity.response.DealerInfoVo;
import com.molichuxing.gateway.bff.dealer.entity.response.DealerVo;
import com.molichuxing.gateway.bff.dealer.services.DealerBffService;
import com.molichuxing.gateway.bff.dealer.services.DealerGoodsBffService;
import com.molichuxing.gateway.bff.goods.entity.response.GoodsVo;
import com.molichuxing.gateway.property.DealerStatusEnum;
import com.molichuxing.services.business.dto.response.DealerBizDto;
import com.molichuxing.services.business.dto.response.DealerDetailInfoBizDto;
import com.molichuxing.services.business.dto.response.DealerInfoBizDto;
import com.molichuxing.services.business.dto.response.GoodsBizBizDto;
import com.molichuxing.services.business.service.DealerBizService;
import com.molichuxing.services.business.service.GoodsBizService;
import com.molichuxing.services.business.service.VehicleBizService;
import com.molichuxing.services.infrastructure.dto.response.GoodsDto;
import com.molichuxing.services.infrastructure.dto.response.RegionInfoDto;
import com.molichuxing.services.infrastructure.service.DealerService;
import com.molichuxing.services.infrastructure.service.GoodsAuthService;
import com.molichuxing.services.infrastructure.service.RegionService;
import com.molichuxing.services.property.GoodsTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 经销商信息相关bff
 *
 * @author zhaopenghui
 * @date 2019年8月26日
 */
@Service("dealerGoodsBffService")
public class DealerGoodsBffServiceImpl implements DealerGoodsBffService {
    private static final Logger logger = LoggerFactory.getLogger(DealerGoodsBffServiceImpl.class);

    @Resource
    private DealerBizService dealerBizService;

    @Resource
    private RegionService regionService;

    @Resource
    private GoodsBizService goodsBizService;

    @Resource
    private GoodsAuthService goodsAuthService;

    @Resource
    private VehicleBizService vehicleBizService;


    @Override
    public Paged<DealerGoodsVo> getDealerGoodsPage(Map<String, Object> params, Integer pageNum, Integer pageSize) throws Exception {
        params.put("sortName",null);
        params.put("sortWay",null);
        Paged<DealerBizDto> dealerPage = dealerBizService.getDealerPage(params, pageNum, pageSize);
        List<DealerBizDto> dealerBizDtoList = dealerPage.getList();
        if (null == dealerBizDtoList) {
            return new Paged<DealerGoodsVo>();
        }

        List<Integer> regionIdList = new ArrayList<>();
        for (DealerBizDto dealerBizDto : dealerBizDtoList) {
            Integer regionId = dealerBizDto.getRegionId();
            regionIdList.add(regionId);
        }

        Map<Integer, RegionInfoDto> regionInfoMap = regionService.getRegionInfoListByIds(regionIdList);
        Map<Integer, List<GoodsDto>> goodsMap = goodsBizService.getGoodsListByDealerIds(getDealerIds(dealerBizDtoList));
        List<DealerGoodsVo> dealerVoList = Convert.toDealerGoodsVoList(dealerBizDtoList, regionInfoMap, goodsMap);

        Paged<DealerGoodsVo> paged = new Paged<DealerGoodsVo>(dealerVoList, dealerPage.getTotal(), pageSize);
        return paged;
    }

    @Override
    public List<GoodsVo> getDealerGoodsList(Integer dealerId, Integer goodsType, Integer carStyleId) {
        List<GoodsBizBizDto> goodsList = goodsBizService.getDealerGoodsList(dealerId, goodsType == null ? null : GoodsTypeEnum.getName(goodsType), carStyleId);
        Map<Integer, String> vehicleMap = new HashMap<>();
        if(null != goodsList && goodsList.size() > 0) {
            for(GoodsBizBizDto goodsBizBizDto : goodsList) {
                String vehicleName = vehicleBizService.getVehicleName(goodsBizBizDto.getVehicleStyleId());
                vehicleMap.put(goodsBizBizDto.getGoodsId(), vehicleName);
            }
        }
        return com.molichuxing.gateway.bff.goods.convertor.Convert.toGoodsVo(goodsList, vehicleMap, null);
    }

    @Override
    public Boolean modifyGoodsDealer(List<Integer> goodsIds, List<Integer> dealerIds, List<Integer> unGoodsIds) throws Exception{
        if((null != dealerIds)&&(dealerIds.size() > 0)) {
            for(Integer dealerId : dealerIds) {
                if((null != unGoodsIds)&&(unGoodsIds.size() > 0)) {
                    goodsAuthService.deleteDealerRelation(dealerId,unGoodsIds);
                }
                goodsAuthService.createGoodsDealer(goodsIds, Arrays.asList(dealerId));
            }
        }
        return true;
    }

    private List<Integer> getDealerIds(List<DealerBizDto> dealerVoList) {
        List<Integer> ids = null;
        if(null != dealerVoList&&dealerVoList.size() > 0) {
            ids = new ArrayList<>();
            for(DealerBizDto dealerGoodsVo : dealerVoList) {
                ids.add(dealerGoodsVo.getDealerId());
            }
        }
        return ids;
    }
}
